package com.thoughtworks.opensource.yui.dd.client.triads.incomingnumbers;
/*
   Copyright 2006 C. K. Sidu Ponnappa

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.thoughtworks.opensource.yui.dd.client.Dd;
import com.thoughtworks.opensource.yui.dd.client.Listener;
import com.thoughtworks.opensource.yui.dd.client.domain.MyNumber;
import com.thoughtworks.opensource.yui.dd.client.domain.MyNumberView;
import com.thoughtworks.opensource.yui.dd.client.events.Event;
import com.thoughtworks.opensource.yui.dd.client.events.EventType;
import com.thoughtworks.opensource.yui.dd.client.helpers.DOMUtils;
import com.thoughtworks.opensource.yui.dd.client.helpers.Factory;
import com.thoughtworks.opensource.yui.dd.client.windowresources.DisplayAreaController;
import com.thoughtworks.opensource.yui.dd.client.windowresources.DisplayAreaViewBase;

/**
 * Created by IntelliJ IDEA.
 * Author: C. K. Sidu Ponnappa
 * Date: Nov 23, 2006
 * Time: 1:24:36 AM
 * ThoughtWorks India Pvt. Ltd.
 */
public class IncomingNumbersPanel extends FlowPanel implements Listener {
    private DisplayAreaViewBase hostView;
    private DisplayAreaController hostController;
    private IncomingNumbersModel model;

    public IncomingNumbersPanel(DisplayAreaViewBase hostView, DisplayAreaController hostController, IncomingNumbersModel model) {
        this.hostView = hostView;
        this.hostController = hostController;
        this.model = model;
        this.model.addListener(this);
        registerListeners();
    }

    private void registerListeners() {
        this.hostController.registerListenerFor(this, EventType.DROP_OCCURED);
    }

    private String getId() {
        return DOMUtils.getIdFor(this);
    }

    public void notify(Event event) {
        if (event.is(EventType.MODEL_CHANGED)) {
            refresh();
        } else if (event.is(EventType.DROP_OCCURED)) {
            Widget widget = (Widget) event.getPayload();
            GWT.log("Numbers Panel " + this.getId() + " notified of drop via registry: " + ((MyNumberView) hostView.getModelForWidget(widget)).getNumber(), null);
        }
    }

    private void refresh() {
        this.clear();
        for (int i = 0; i < model.getCount(); i++) {
            MyNumber myNumber = model.get(i);
            Label label = Factory.createLabel(getId() + "_mynumber_" + i, Dd.intToString(((MyNumberView) myNumber).getNumber()), "myNumberLabel");
            hostView.addToDraggables(label);
            hostView.mapWidgetToModel(label, myNumber);
            hostView.addWidgetToPanelWithMapping(label, this);
        }
    }
}
